home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / sin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  874 b   |  54 lines

  1.  
  2. /* sin, expansion due to good ol Hastings */
  3.  
  4. #include "math.h"
  5.  
  6. #define N_COEFF 5
  7.  
  8. double _sin_coeff[] = 
  9.     {
  10.     1.57079631847,
  11.     -.64596371106,
  12.     .07968967928,
  13.     -.00467376557,
  14.     .00015148419
  15.     };
  16.  
  17. double sin(x)
  18. double x;
  19. {
  20.   double x2;        /* will be x^2 */
  21.   double xn;        /* will be x^(2n+1) */
  22.   double accum;
  23.   int n;
  24.   long ix;
  25.  
  26.   x = x / (pi * 2);        /* scale it */
  27. /* need to fold x back to between -1 and 1 */
  28.  
  29.   ix = (long )x;        /* truncate to int */
  30.   x = x - (double )ix;        /* subtract the int part */
  31.                 /* now it's between -1 and 1 */
  32.  
  33.   x = (x * 4.0);
  34.  
  35.   while (x > 1.0)
  36.     x = 2.0 - x;
  37.   while (x < -1.0)
  38.     x = -2.0 - x;
  39.  
  40.   x2 = x * x;        /* x^2 */
  41.  
  42.   for (n = 0, xn = x, accum = 0 ; n < N_COEFF ; n++)
  43.     {
  44.     accum += xn * _sin_coeff[n];
  45.     xn = xn * x2;
  46.     }
  47.   return(accum);
  48. }
  49.  
  50. double cos(x)
  51. {
  52.   return(sin(x + (pi / 2.0)));
  53. }
  54.